package com.judysen.jsqlweb.controller;

import com.judysen.jsqlweb.aop.JsqlLoggerAnnotation;
import com.judysen.jsqlweb.models.Pager;
import com.judysen.jsqlweb.models.RestResult;
import com.judysen.jsqlweb.services.JsqlManagerService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/jsql")
public class JsqlController {
    @Resource
    private JsqlManagerService jsqlManagerService;

    /**
     * 获取当前库里所有的数据表
     *
     * @return
     */
    @JsqlLoggerAnnotation
    @RequestMapping(value = "/table", method = RequestMethod.GET)
    public RestResult getTableNames() {
        var tables = jsqlManagerService.getAllTables();
        return new RestResult(200, "", tables);
    }

    /**
     * 获取表信息
     *
     * @param table
     * @return
     */
    @JsqlLoggerAnnotation
    @RequestMapping(value = "/{table}/columns", method = RequestMethod.GET)
    @ResponseBody
    public RestResult getColumns(@PathVariable String table) {
        var columns = jsqlManagerService.getColumns(table);
        return new RestResult(200, "", columns);
    }

    @JsqlLoggerAnnotation
    @RequestMapping(value = "/{table}/values", method = RequestMethod.GET)
    @ResponseBody
    public RestResult getTableValues(@PathVariable String table, HttpServletRequest request) {
        var pager = new Pager();
        var pageIndex = request.getParameter("pageIndex");
        var pageSize = request.getParameter("pageSize");
        if (StringUtils.isEmpty(pageIndex)) {
            pager.setPageIndex(0);
        } else {
            pager.setPageIndex(Integer.parseInt(pageIndex));
        }
        if (StringUtils.isEmpty(pageSize)) {
            pager.setPageSize(20);
        } else {
            pager.setPageSize(Integer.parseInt(pageSize));
        }

        pager.setTable(table);
        var pages = jsqlManagerService.findByPage(pager);

        return new RestResult(200, "", pages);
    }

    @JsqlLoggerAnnotation
    @RequestMapping(value = "{table}", method = RequestMethod.POST)
    @ResponseBody
    public RestResult save(@PathVariable String table, @RequestBody Map<String, Object> map) {
        var flag = jsqlManagerService.insert(table, map);
        return new RestResult(200, "", flag);
    }

    @JsqlLoggerAnnotation
    @RequestMapping(value = "/{table}/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public RestResult update(@PathVariable String table, @PathVariable Object id, @RequestBody Map<String, Object> map) {
        var flag = jsqlManagerService.update(table, id, map);
        return new RestResult(200, "", flag);
    }

    @JsqlLoggerAnnotation
    @RequestMapping(value = "/{table}/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public RestResult delete(@PathVariable String table, @PathVariable String id) {
        var flag = jsqlManagerService.delete(table, id);
        return new RestResult(200, "", flag);
    }
}
